<?php
namespace app\common\model;

class UserAccount extends BaseModel{
	public $tips_info = '';
	
	/**
	 * 判断用户是否可以提现
	 * @param  [type]  $user_id 用户ID
	 * @param  [type]  $money   提现金额
	 */
	public function is_take_out($user_id, $money){
		if(is_numeric($money) == false || $money < 0){
			$this->tips_info = '对不起，您的金额有误！';
			return false;
		}
		if($money < 50){
			$this->tips_info = '最低提现金额为50！';
			return false;
		}
		
		if($this->get_account_money($user_id, 'usable_account') < $money){
			$this->tips_info = '对不起，您要提现的金额超过账户余额！';
			return false;
		}

		$num  = 2;//提现次数
		$week = date('N');//星期中的第几天，数字
		// if(!in_array($week, array('6', '7'))){
		// 	$this->tips_info = '对不起，只能周六或周日才能提现！';
		// 	return false;
		// }
		// $week = 7;
		//开始时间
		$start_time = strtotime('-' . ($week - 1) . 'day');
		//结束时间
		$end_time   = strtotime('+' . (8 - $week) . 'day');

		$start_date = date('Y-m-d', $start_time);
		$end_date   = date('Y-m-d', $end_time);
		//转换成正常0点时间
		$start_time = strtotime($start_date);
		$end_time   = strtotime($end_date);

		//判断两周之前到现在的是否提现过
		$where['user_id']       = $user_id;
		$where['create_time'][] = array('egt', $start_time);
		$where['create_time'][] = array('lt', $end_time);
		$where['status']        = array('neq', 3);//提现失败的不算
		$count = db('UserTakeOut')->where($where)->count();
		//判断是否可以提现
		if($count >= $num){
			$this->tips_info = '对不起，您这一周内已有提现过' . $num . '次了！';
			return false;
		}

		return true;
	}
	/**
	 * 获取用户账户信息
	 * @param  [type] $user_id 用户ID
	 */
	public function get_user_account_info($user_id){
		$data['usable_account']   = $this->get_account_money($user_id, 'usable_account');// 可用账户
		$data['used_account']     = $this->get_account_money($user_id, 'used_account');// 已用账户
		$data['blocked_account']  = $this->get_account_money($user_id, 'blocked_account');// 冻结帐户
		$data['take_out_account'] = $this->get_account_money($user_id, 'take_out_account');// 提现账户

		return $data;
	}
	
	/**
	 * 提交问题扣除悬赏
	 * @param  [type] $user_id  用户ID
	 * @param  string $money    金额
	 * @param  string $ask_id   问题ID
	 */
	public function ask_submit($user_id, $money = '', $ask_id = ''){
		if(!$user_id || $money < 0 || !$ask_id){
			return false;
		}
		//增加已用账户
		$this->update_account_money($user_id, $money, 'beans_used');
		//添加账单
		$this->add_bill($user_id, 'ask_submit', $money, 'beans_used', '', '', $ask_id);

		//扣除账户
		$this->update_account_money($user_id, -$money, 'beans_usable');
		//添加账单
		$this->add_bill($user_id, 'ask_submit', -$money, 'beans_usable', '', '', $ask_id);

		// 更新豆子
		model('UserData')->update_user_data($user_id, 'beans', -$money);

		return true;
	}
	/**
	 * 回答问题增加
	 * @param  [type] $user_id  用户ID
	 * @param  string $money    金额
	 * @param  string $ask_id   问题ID
	 */
	public function ask_answer($user_id, $money = '', $ask_id = ''){
		if(!$user_id || $money < 0 || !$ask_id){
			return false;
		}
		//增加到账户
		$this->update_account_money($user_id, $money, 'beans_usable');
		//添加账单
		$this->add_bill($user_id, 'answer_reward', $money, 'beans_usable', '', '', $ask_id);

		// 更新豆子
		model('UserData')->update_user_data($user_id, 'beans', $money);

		return true;
	}
	/**
	 * 账户充值
	 * @param  [type] $user_id  用户ID
	 * @param  [type] $pay_type 支付类型
	 * @param  string $money    金额
	 * @param  string $order_sn 订单号
	 */
	public function recharge($user_id, $pay_type, $money = '', $order_sn = ''){
		if(!$user_id || !$pay_type || $money < 0 || !$order_sn){
			return false;
		}
		//充值到账户
		$this->update_account_money($user_id, $money, 'usable_account');
		//添加账单
		$this->add_bill($user_id, 'recharge', $money, 'usable_account', $pay_type, $order_sn);

		return true;
	}
	/**
	 * 提现
	 * @param  [type] $user_id 用户ID
	 * @param  string $money   提现金额
	 */
	public function take_out($user_id, $money = '', $take_out_id){
		if(!$user_id || !$money){
			return false;
		}
		//判断账户余额是否够提现
		$usable_money = $this->get_account_money($user_id, 'usable_account');
		if($usable_money < $money){
			return false;
		}
		//扣除可用账户金额
		$this->update_account_money($user_id, -$money, 'usable_account');
		//添加扣除可用账户记录
		$this->add_bill($user_id, 'take_out', -$money, 'usable_account', '', '', $take_out_id);
		//添加已用账户增加记录
		$this->add_bill($user_id, 'take_out', $money, 'take_out_account', '', '', $take_out_id);

		return true;
	}
	/**
	 * 提现失败处理
	 * @param  [type] $user_type 用户类型
	 * @param  [type] $user_id   用户ID
	 * @param  string $money     金额
	 */
	public function take_out_refund($user_id, $money = '', $remark = ''){
		if(!$user_id || !$money){
			return false;
		}
		$remark = '提现失败->' . $remark;
		// 增加可用金额
		$this->update_account_money($user_id, $money, 'usable_account');
		// 生成账单
		$this->add_bill($user_id, 'take_out_refund', $money, 'usable_account', '', '', '', $remark);

		// 扣除提现账户
		$this->update_account_money($user_id, -$money, 'take_out_account');
		// 生成账单
		$this->add_bill($user_id, 'take_out_refund', -$money, 'take_out_account', '', '', '', $remark);

		return true;
	}
	
	/**
	 * 支付订单
	 * @param  string $pay_type   支付类型
	 * @param  [type] $user_id    用户ID
	 * @param  [type] $order_sn   订单号
	 * @param  string $product_id 产品ID
	 * @param  [type] $money      支付金额
	 * @param  string $remark     备注
	 */
	public function pay_order($pay_type = 'alipay', $user_id, $order_sn, $product_id = '', $money, $remark = ''){
		if(!$user_id || !$order_sn || $money < 0){
			return false;
		}

		//用户已用账户增加金额
		$this->update_account_money($user_id, $money, 'used_account');
		//添加已用账户账单
		$this->add_bill($user_id, 'pay_product_order', $money, 'used_account', $pay_type, $order_sn, $product_id, $remark);

		if($this->company_id){
			//公司已用账户增加金额
			$this->update_account_money($this->company_id, $money, 'company_used_account');
		}
		return true;
	}
	/**
	 * 余额支付
	 * @param  [type] $user_id    用户ID
	 * @param  [type] $order_sn   订单号
	 * @param  [type] $product_id 职位ID
	 * @param  [type] $money      支付金额
	 * @param  string $remark     备注
	 */
	public function balance_pay($user_id, $order_sn, $product_id, $money, $remark = ''){

		// 扣除可用余额账户
		$this->update_account_money($user_id, -$money, 'usable_account');
		// 生成账单
		$this->add_bill($user_id, 'pay_order', -$money, 'usable_account', 'balance_pay', $order_sn, $product_id, $remark);

		// 增加冻结账户余额
		$this->update_account_money($user_id, $money, 'used_account');
		// 生成账单
		$this->add_bill($user_id, 'pay_product_order', $money, 'used_account', 'balance_pay', $order_sn, $product_id, $remark);

		return true;
	}
	/**
	 * 取消订单
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $order_sn     订单号
	 * @param  [type] $product_id	产品ID
	 * @param  [type] $money        订单金额
	 * @param  string $remark       备注
	 */
	public function cancel_order($user_id, $order_sn, $money, $product_id = 0, $remark = ''){
		if(!$user_id || !$order_sn || $money < 0){
			return false;
		}

		//扣除用户已用账户金额
		$this->update_account_money($user_id, -$money, 'used_account');
		//添加扣除已用账户账单
		$this->add_bill($user_id, 'cancel_product_order', -$money, 'used_account', '', $order_sn, $product_id, $remark);

		//可用账户增加金额
		$this->update_account_money($user_id, $money, 'usable_account');
		//添加账单
		$this->add_bill($user_id, 'cancel_product_order', $money, 'usable_account', '', $order_sn, $product_id, $remark);

		return true;
	}
	/**
	 * 订单退款处理
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $order_sn     订单号
	 * @param  [type] $product_id	产品ID
	 * @param  [type] $money        订单金额
	 * @param  string $remark       备注
	 */
	public function order_refund($user_id, $order_sn, $money, $product_id = 0, $remark = ''){
		if(!$user_id || !$order_sn || $money < 0){
			return false;
		}

		//扣除用户已用账户金额
		$this->update_account_money($user_id, -$money, 'used_account');
		//添加扣除已用账户账单
		$this->add_bill($user_id, 'order_refund', -$money, 'used_account', '', $order_sn, $product_id, $remark);

		if($this->company_id){
			//公司已用账户增加金额
			$this->update_account_money($this->company_id, -$money, 'company_used_account');
		}
		return true;
	}
	
	/**
	 * 开通账户
	 * @param  [type] $user_id      用户ID
	 * @param  string $account_type 账户类型
	 */
	public function create_account($user_id, $account_type = 'usable_account'){
		if(!$user_id || !$account_type){
			return false;
		}
		if(!$this->is_have_account($user_id, $account_type)){
			$data['user_id']      = $user_id;
			$data['account_type'] = $account_type;
			$data['create_time']  = time();
			$this->insert($data);
		}

		return true;
	}
	/**
	 * 判断是否开通账户
	 * @param  [type]  $user_id      用户ID
	 * @param  string  $account_type 账户类型
	 */
	public function is_have_account($user_id, $account_type = 'usable_account'){
		$where['user_id']      = $user_id;
		$where['account_type'] = $account_type;
		$count = $this->where($where)->count();

		return $count > 0;
	}
	/**
	 * 获取账户金额
	 * @param  [type] $user_id      用户ID
	 * @param  string $account_type 账户类型
	 */
	public function get_account_money($user_id, $account_type = 'usable_account'){
		$where['user_id']      = $user_id;
		$where['account_type'] = $account_type;
		$money = $this->where($where)->value('money');

		return $money ? sprintf("%.2f", $money) : '0.00';
		// return $money ? number_format($money, 2, '.', ',') : '0.00';
	}
	
	/**
	 * 更新账户金额
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $account_type 账户类型
	 * @param  [type] $money        金额
	 */
	private function update_account_money($user_id, $money, $account_type = 'usable_account'){
		if(!$this->create_account($user_id, $account_type)){
			return false;
		}
		if($money == 0){
			return false;
		}
		
		if($money > 0){
			$data['money'] = array('exp','money+' . abs($money));
		}else{
			$data['money'] = array('exp','money-' . abs($money));
		}
		$data['edit_time'] = time();
		
		$where['user_id']      = $user_id;
		$where['account_type'] = $account_type;
		$this->where($where)->update($data);

		return true;
	}
	/**
	 * 添加账单
	 * @param [type] $user_id      用户ID
	 * @param [type] $bill_type    账单类型
	 * @param [type] $money        账单金额
	 * @param string $account_type 账户类型
	 * @param string $pay_type     支付类型
	 * @param string $order_sn     订单号
	 */
	private function add_bill($user_id, $bill_type, $money, $account_type = 'usable_account', $pay_type = 'weixin', $order_sn ='', $other_id = '', $remark = ''){
		if($order_sn){
			$city_info = model('Order')->get_city_info($order_sn);
		}
		$app_type   = $this->getParams('app_type');
		$city_info  = $city_info ? $city_info : model('User')->get_user_city_info($user_id);
		$data['province_id']  = $city_info['province_id'];
		$data['city_id']      = $city_info['city_id'];
		$data['user_id']      = $user_id;
		$data['money']        = $money ? $money : 0;
		$data['bill_type']    = $bill_type;
		$data['account_type'] = $account_type;
		$data['balance']      = $this->get_account_money($user_id, $account_type);
		$data['order_sn']     = $order_sn ? $order_sn : '';
		$data['other_id']     = $other_id ? $other_id : 0;
		$data['pay_type']     = $pay_type ? $pay_type : 'system';
		$data['app_type']     = $app_type ? $app_type : 'web';
		$data['remark']       = $remark ? $remark : '';
		$data['create_time']  = time();

		$model = db('UserBill');
		$model->insert($data);

		return $model->getLastInsID();
	}

}